package com.pie4cloud.pie.job.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.pie4cloud.pie.common.core.util.R;
import com.pie4cloud.pie.common.log.annotation.SysLog;


import com.pie4cloud.pie.job.api.entity.GatherEntity;
import com.pie4cloud.pie.job.biz.service.GatherPageService;
import com.pie4cloud.pie.job.biz.service.GatherService;
import com.pie4cloud.pie.job.biz.thread.ScheduleThread;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequiredArgsConstructor
@RequestMapping("/gather" )
public class GatherController {
	private final GatherService gatherService;

	private final ScheduleThread scheduleThread;

	@Resource
	private GatherPageService gatherPageService;

//	@ApiOperation(value = "分页查询", notes = "分页查询")
//	@GetMapping("/page" )
//	public R getTDataJobInfoPage(Page page, GatherEntity gatherEntity) {
//		return R.ok(gatherService.page(page,gatherEntity));
//	}

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param gatherEntity
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page" )
	public R getConfigGatherPage(Page page, GatherEntity gatherEntity) {
		return R.ok(gatherPageService.selectGatherPage(page, Wrappers.query(gatherEntity)));
	}

	/**
	 * 查询所有
	 * @return
	 */
	@ApiOperation(value = "查询所有", notes = "查询所有")
	@GetMapping("/list" )
	public R list() {
		return R.ok(gatherService.list());
	}

	/**
	 * 通过id查询
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}" )
	public R getById(@PathVariable("id" ) Long id) {
		return R.ok(gatherService.getById(id));
	}

	/**
	 * 新增
	 * @param gatherEntity
	 * @return R
	 */
	@ApiOperation(value = "新增", notes = "新增")
	@SysLog("新增" )
	@PostMapping
	public R save(@RequestBody GatherEntity gatherEntity) {
		return R.ok(gatherService.save(gatherEntity));
	}

	/**
	 * 修改
	 * @param gatherEntity
	 * @return R
	 */
	@ApiOperation(value = "修改", notes = "修改")
	@SysLog("修改" )
	@PutMapping
	public R updateById(@RequestBody GatherEntity gatherEntity) {
		return R.ok(gatherService.update(gatherEntity));
	}

	/**
	 * 通过id删除
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除", notes = "通过id删除")
	@SysLog("通过id删除" )
	@DeleteMapping("/{id}" )
	public R removeById(@PathVariable Long id) {
		return R.ok(gatherService.removeById(id));
	}


	@ApiOperation(value = "立即执行", notes = "立即执行")
	@SysLog("立即执行任务" )
	@GetMapping("/execute/{id}" )
	public R execute(@PathVariable Long id) {
		GatherEntity gatherEntity=gatherService.getById(id);
		gatherEntity.setType("GATHER");
		gatherEntity.setTriggerLastTime(System.currentTimeMillis());
		scheduleThread.handT(gatherEntity);
		return R.ok();
	}
}
